//
//  TableViewCellEx.m
//  PriceOnline
//
//  Created by Phuong Le on 8/30/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "TableViewCellEx.h"
#import "UIConstants.h"

@implementation TableViewCellEx

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        [self reloadTheme];
        
        _subView = [[UIView alloc] init];
        _subView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_subView];
        
        _lineView = [[LineView alloc]init];
        [_subView addSubview:_lineView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:NOTIFICATION_CHANGE_THEME object:nil];
    }
    
    return self;
}

- (void)reloadTheme
{
    if ([DataSingleton shared].isDarkTheme)
    {
        self.contentView.backgroundColor = COLOR_HEX(0x3C3C3C);
        _textColor = COLOR_WHITE_NORMAL;
        _highlightBackgroundColor = COLOR_LIGHT_BLACK;
    }
    else
    {
        self.contentView.backgroundColor = COLOR_BACKGROUND_CELL_LIGHT;
        _textColor = COLOR_BLACK_NORMAL;
        _highlightBackgroundColor = [UIColor lightGrayColor];
    }
    
    self.selected = self.selected;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
    _subView.backgroundColor = selected? _highlightBackgroundColor : [UIColor clearColor];
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    if (highlighted)
    {
        _subView.backgroundColor = _highlightBackgroundColor;
    }
    else
    {
        if (!self.selected)
        {
            _subView.backgroundColor = [UIColor clearColor];
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _subView.frame = RECT(0, 0, self.contentView.width, self.contentView.height);
    _lineView.frame = RECT(0, 0, _subView.width, 2);
}

- (void)setLable:(id)label forLabel:(UILabel*)labelControl
{
    NSString *text = @"";
    
    if (label && [label isKindOfClass:[NSString class]])
    {
        text = label;
    }
    
    [labelControl setText:text];
}

- (void)handleNotification:(NSNotification *)sender
{
    [self reloadTheme];
}

@end
